/**
 * Created by hda on 2017/6/13.
 */
define(['ks-spa', 'jquery', 'vue'], function (ksspa, $, Vue) {
    var model = {};
    model.init = function (m) {

        ksspa.start({
            view: m.el,
            errorTemplateId: '#error', // 可选
            router: {
                'index': {view: root_path + '/user/address/index'},
                'addressSelect': {view: root_path + '/user/address/select', controller: 'user/address/select'},
                'addressPost': {view: root_path + '/user/address/post', controller: 'user/address/post'},
                'addressEdit': {view: root_path + '/user/address/edit', controller: 'user/address/edit'},
                'addressLocation': {
                    view: root_path + '/user/address/location',
                    controller: 'user/address/location'
                },
                'addressMap': {view: root_path + '/user/address/map', controller: 'user/address/map'},
                'default': 'index' //默认路由
            }
        });


        var $list_data = document.getElementById('list_data').value;
        var vue = gvue = new Vue({
            el: "#el-address-select",
            data: {
                list: eval($list_data),
                address_id: 0
            },
            methods: {
                edit: function (id) {
                    // console.log(id);
                    this.rebind();
                },
                del: function (id) {

                    mui.confirm('确定删除地址？', '', ['取消', '确定'], function (e) {
                        if (e.index == 1) {

                            $.post(root_path + '/user/address/delete', {id: id}, function (json) {
                                mui.toast(json.msg);
                                if (json.flag) {
                                    // $(event.currentTarget).parents('ul').remove()
                                    $("#ul_address_" + id).remove();
                                    if (typeof (json.address) != 'undefined') {
                                        $("#ul_address_" + json.address.id).addClass('active');
                                    }
                                }
                            })

                        }
                    })
                },
                choose: function (m) {
                    $(event.currentTarget.parentNode.parentNode.children).removeClass('active');
                    event.currentTarget.parentNode.className = 'active';
                    mui.toast('已设为默认地址');
                    $.post(root_path + '/user/address/set_default', {id: m.id},function (res) {
                        window.location.href = back_url;
                    })
                },
                rebind: function () {
                    vue.list={};
                    $.get(root_path + '/user/address/select_data', function (json) {
                        vue.list = json;
                    })
                }
            },
            mounted: function () {
                this.address_id = $("#address_id").val();
            }
        })


    }
    return model;
});